package com.admin.utils;

import java.util.Random;

/**
 * Created by yzl on 2016/10/27.
 */
public class RandomStrUtil {

    public static String getRandAllStr(int num){
        String str="";
        Random random = new Random();
        for(int i = 0; i < num; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                str += (char)(random.nextInt(26) + 97);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                str += String.valueOf(random.nextInt(10));
            }
        }
        return str;
    }

    public static String getRandStr(int num){
        String str="";
        Random random = new Random();
        for(int i = 0; i < num; i++) {
           str += (char)(random.nextInt(26) + 97);
        }
        return str;
    }

    public static String getRandInt(int num){
        String str="";
        Random random = new Random();
        for(int i = 0; i < num; i++) {
            str += String.valueOf(random.nextInt(10));
        }
        return str;
    }

    public static void main(String[] args) {
        System.out.println(RandomStrUtil.getRandAllStr(6));
        System.out.println(RandomStrUtil.getRandStr(6));
        System.out.println(RandomStrUtil.getRandInt(6));
    }
}
